﻿#include "DAUtilNodeFactory.h"
#include "DAVariantValueNode.h"
#include "DATestNode.h"
DAUtilNodeFactory::DAUtilNodeFactory(QObject *p) : DAAbstractNodeFactory(p)
{
    createMetaData();
}

QString DAUtilNodeFactory::factoryName() const
{
    return ("FC.Util");
}

DAAbstractNode *DAUtilNodeFactory::create(const DANodeMetaData &meta)
{
    FpCreate fp = m_prototypeTpfp.value(meta, nullptr);

    if (fp) {
        return (fp());
    }
    return (nullptr);
}

QStringList DAUtilNodeFactory::getPrototypes() const
{
    QStringList res;

    res.reserve(m_prototypeTpfp.size());
    for (auto i = m_prototypeTpfp.begin(); i != m_prototypeTpfp.end(); ++i) {
        res.append(i.key().getNodePrototype());
    }
    return (res);
}

QList<DANodeMetaData> DAUtilNodeFactory::getNodesMetaData() const
{
    return (m_prototypeTpfp.keys());
}

void DAUtilNodeFactory::createMetaData()
{
    FpCreate fp;

    // FCConstValueNodeGraphicsItem
    fp = []() -> DAAbstractNode * { return (new DAVariantValueNode()); };
    m_prototypeTpfp[saveGetMetaType(fp())] = fp;
    // FCTestNodeGraphicsItem
    fp = []() -> DAAbstractNode * { return (new DATestNode()); };
    m_prototypeTpfp[saveGetMetaType(fp())] = fp;
}

/**
 * @brief 这个主要是为了获取到FCAbstractNodeGraphicsItem对应的metadata，获取完metadata后把内存销毁
 * @param t
 * @return
 */
DANodeMetaData DAUtilNodeFactory::saveGetMetaType(DAAbstractNode *t)
{
    DANodeMetaData meta = t->metaData();

    delete t;
    return (meta);
}
